package com.magic.smartcity.view.more.activity

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.google.android.material.snackbar.Snackbar
import com.magic.smartcity.R
import com.magic.smartcity.databinding.FragmentActivityDetailBinding
import com.magic.smartcity.utils.Constant
import com.magic.smartcity.view.MainActivity
import com.magic.smartcity.view.more.adapters.ActivityDetailCommentAdapter
import com.magic.smartcity.view.more.adapters.ActivityHomeListAdapter
import com.magic.smartcity.view.more.viewmodels.ActivityViewModel
import kotlin.properties.Delegates

class ActivityDetailFragment : Fragment() {
    lateinit var binding: FragmentActivityDetailBinding
    var activityId by Delegates.notNull<Int>()
    private val model: ActivityViewModel by lazy { ViewModelProvider(this).get(ActivityViewModel::class.java) }

    private val commentAdapter = ActivityDetailCommentAdapter()
    private val recommendAdapter = ActivityHomeListAdapter()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activityId = arguments?.getInt("id")!!
        if (id != null) {
            model.getActivityById(activityId)
            model.getActivityCommentById(activityId)
            //判断用户是否已登录，登录的话再请求获取用户是否已经报名活动状态
            if (Constant.TOKEN!=null) {
                model.userIsSignup(activityId)
            }
        }
        model.getActivityList()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentActivityDetailBinding.inflate(layoutInflater)


        initView()
        return binding.root
    }

    private fun initView() {
        //活动详情
        val mainActivity = activity as MainActivity
        model.activityDetail.observe(viewLifecycleOwner) {
            Glide.with(binding.imageView2).load(Constant.Service_IP + it.imgUrl).error(R.color.gray)
                .into(binding.imageView2)
            binding.webView.loadData(it.content, "text/html", "utf-8")
            mainActivity.setActionBarTitle(it.name)
            binding.activityDetail = it
        }

        //推荐列表
        binding.commentRy.adapter = commentAdapter
        binding.commentRy.layoutManager =
            LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        model.commentList.observe(viewLifecycleOwner) {
            commentAdapter.setData(it)
            binding.commentSize = it.size
        }

        //活动评论列表
        binding.recommendRy.adapter = recommendAdapter
        binding.recommendRy.layoutManager =
            LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        model.activityList.observe(viewLifecycleOwner) {
            val filter = it.filter { it.recommend.equals("Y") }
            recommendAdapter.setData(filter)
        }

        //评论
        binding.submitComment.setOnClickListener {
            if (binding.commentContent.text.isNotEmpty())
                model.activityComment(activityId, binding.commentContent.text.toString())
        }
        model.isCommentOk.observe(viewLifecycleOwner) {
            Snackbar.make(binding.root, it, Snackbar.LENGTH_SHORT).show()
            if (it.equals("评论成功!")) {
                binding.commentContent.text.clear()
            }
        }
        model.error.observe(viewLifecycleOwner) {
            if (it == 401) {
                Snackbar.make(binding.root, "未登录!", Snackbar.LENGTH_SHORT).show()
            } else {
                Snackbar.make(binding.root, "未知错误!", Snackbar.LENGTH_SHORT).show()
            }
        }


        //立刻报名,检测用户是否登录,未登录状态下提示登录
        if (Constant.TOKEN==null) {
            binding.signup.setOnClickListener {
                Snackbar.make(binding.root, "未登录!", Snackbar.LENGTH_SHORT).show()
            }
        }
        model.isSignup.observe(viewLifecycleOwner) { it ->
            if (it) {
                binding.signup.text = "已经报名"
                binding.signup.setOnClickListener(null)
            } else {
                binding.signup.text = "立刻报名"
                binding.signup.setOnClickListener {
                    //立刻报名,检查用户是否登录,
                    if (Constant.TOKEN!=null) {
                        model.signup(activityId)
                        model.signOk.observe(viewLifecycleOwner) {
                            Snackbar.make(binding.root, it, Snackbar.LENGTH_SHORT).show()
                            binding.signup.text = "已经报名"
                            binding.signup.setOnClickListener(null)
                        }
                    } else {
                        Snackbar.make(binding.root, "未登录!", Snackbar.LENGTH_SHORT).show()
                    }
                }
            }
        }

    }

}